﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;
using CUREEntity;
using System.Data;
using CUREDataAccessLayers.Common;
namespace CUREDataAccessLayers
{
    public class ConfigDal
    {
        private common comm = new common();

        /// <summary>
        /// 菜单信息分页方法
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">每页显示的数据条数</param>
        /// <param name="totalRecordCount">总记录数</param>
        /// <param name="totalPageCount">总页数</param>
        /// <returns></returns>
        public DataSet GetConfigData(IDictionary<string, object> where, int pageIndex, int pageSize, out int totalRecordCount, out int totalPageCount)
        {
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select switchcode,switchname,decode(switchvalue,'0','关闭','开启') switchvalue,svalue  ");
            commandText.Append(" from  sys_config  where 1=1 ");

            if (where["name"] != null && where["name"].ToString() != "")
            {
                commandText.Append(" and switchname like '%"+where["name"].ToString().Trim()+"%'");
            }
            try
            {
                ds = PageHelper.GetPagedData(pageIndex, pageSize, commandText.ToString(), "  switchcode asc ", out totalRecordCount, out totalPageCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        /// <summary>
        ///添加菜单信息
        /// </summary>
        /// <param name="id">菜单id</param>
        /// <param name="menuName">菜单名</param>
        /// <returns></returns>
        public int InsertConfig(string switchCode, string switchName, string switchValue, string svalue)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("insert into sys_config(switchcode,switchname,switchvalue,svalue)values('" + switchCode + "','" + switchName.Trim() + "','" + switchValue + "','" + svalue + "')");
                result = DbHelperOra.ExecuteSql(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 更新菜单信息
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="menuName">菜单名称</param>
        /// <returns></returns>
        public int UpdateConfig(string switchCode, string switchName, string switchValue, string svalue)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(switchCode) && !string.IsNullOrEmpty(switchName))
                {
                    commandText.Append("update sys_config set switchname='" + switchName.Trim() + "',switchvalue='" + switchValue + "',svalue='" + svalue + "' where switchcode='" + switchCode.Trim() + "'");
                    result = DbHelperOra.ExecuteSql(commandText.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteMenu(string id)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(id))
                {
                    commandText.Append("delete from actioncolumn where action_columnid='" + id.Trim() + "'");
                    result = DbHelperOra.ExecuteSql(commandText.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 获取最大的id
        /// </summary>
        /// <returns></returns>
        public decimal GetMaxId()
        {
            decimal maxid = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select max(action_columnid) from actioncolumn");
                maxid = DbHelperOra.GetMaxID("action_columnid", "actioncolumn");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return maxid;
        }
        /// <summary>
        /// 根据id获取菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetConfigInfoByCode(string code)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select * from sys_config where switchcode='" + code + "'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据菜单名称获取信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public DataTable GetConfigByCodeName(string code,string name)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select * from sys_config where switchcode='" + code.Trim() + "' or switchname='" + name.Trim() + "'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据id和名称获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public DataTable GetConfigByCodeAndName(string code, string name)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select * from sys_config where switchcode!='" + code + "' and switchname='" + name + "'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
